<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item to="/home">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片试图 -->
    <el-card>
      <el-row>
        <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
      </el-row>

      <!-- 表格 -->
      <tree-table class="tree-table" :data="cateList" :columns="columns" :selection-type="false" :expand-type="false" show-index index-text="#" border :show-row-hover="false">
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color: lightgreen"></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" size="mini" v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag type="warning" size="mini" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <div style="display: none">{{ scope.row }}</div>
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="editCateName(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="delCate(scope.row)">删除</el-button>
        </template>
      </tree-table>

      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </el-card>

    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="50%" @close="addCateDialogColosed">
      <!-- 表单 -->
      <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateRuleForm" label-width="80px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <!-- options指定数据源 -->
          <!-- props指定配置对象 -->
          <el-cascader class="elCascader" v-model="selectdeKeys" :options="parentCateList" :props="cascaderProps" @change="parentCateChange" clearable></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改分类对话框 -->
    <el-dialog title="编辑分类" :visible.sync="editCateDialogVisible" width="50%" @close="editCateDialogColosed">
      <!-- 表单 -->
      <el-form :model="editCateForm" :rules="addCateFormRules" ref="editCateRuleForm" label-width="80px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editCateForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCateList, addCateGories, editCateGories, deleteCateGories } from '@/api'
export default {
  name: 'GoodsCate',
  data() {
    return {
      cateList: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      queryInfoParents: { type: 2 },
      // 父级分类列表
      parentCateList: [],
      // 总数据条数
      total: 0,
      // 为table指定列的定义
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
          // width: '400px'
        },
        {
          label: '是否有效',
          type: 'template',
          // 表示当前这一列使用的模板名称
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          // 表示当前这一列使用的模板名称
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          // 表示当前这一列使用的模板名称
          template: 'opt'
        }
      ],
      // 添加分类对话框
      addCateDialogVisible: false,
      addCateForm: {
        cat_name: '',
        // 父级分类id
        cat_pid: 0,
        // 分类的层级
        cat_level: 0
      },
      // 规则
      addCateFormRules: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      // 指定级联选择器配置对象
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        // 任意级别都可选
        checkStrictly: true
      },
      // 选中的父级分类id
      selectdeKeys: [],
      // 修改分类的名称
      editCateForm: {
        id: '',
        cat_name: ''
      },
      editCateDialogVisible: false
    }
  },
  created() {
    this.getCateLists()
  },
  methods: {
    async getCateLists() {
      const { data: res } = await getCateList(this.queryInfo)
      if (res.meta.status !== 200) return this.$message.error('获取商品分类失败')
      console.log(res.data)
      if (res.data.result.length === 0) {
        this.queryInfo.pagenum--
        if (this.queryInfo.pagenum < 1) {
          return this.$message.warning('暂无数据')
        }
        return this.getCateLists()
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 监听每页显示数据变化
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getCateLists()
    },
    // 页码值改变事件
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getCateLists()
    },
    // 添加分类点击事件
    showAddCateDialog() {
      this.getParentList()
      this.addCateDialogVisible = true
    },
    // 获取父级分类数据列表
    async getParentList() {
      const { data: res } = await getCateList(this.queryInfoParents)
      if (res.meta.status !== 200) return this.$message.error('获取商品分类失败')
      // console.log(res)
      this.parentCateList = res.data
    },
    // 级联选择器变化事件
    parentCateChange() {
      // selectdeKeys.length>0证明选中父级分类，反之没有父级
      if (this.selectdeKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectdeKeys[this.selectdeKeys.length - 1]
        this.addCateForm.cat_level = this.selectdeKeys.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    // 添加分类确定事件
    addCate() {
      console.log(this.addCateForm)
      this.$refs.addCateRuleForm.validate(async (valid) => {
        if (!valid) return ''
        const { data: res } = await addCateGories(this.addCateForm)
        if (res.meta.status !== 201) return this.$message.error('添加分类失败')
        this.$message.success('添加分类成功')
        this.getCateLists()
        this.addCateDialogVisible = false
      })
    },
    // 添加分类关闭事件
    addCateDialogColosed() {
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
      this.selectdeKeys = []
      this.$refs.addCateRuleForm.resetFields()
    },
    // 修改分类名称
    editCateName(val) {
      console.log(val)
      this.editCateDialogVisible = true
      this.editCateForm.id = val.cat_id
      this.editCateForm.cat_name = val.cat_name
    },
    // 修改分类弹窗关闭事件
    editCateDialogColosed() {
      this.$refs.editCateRuleForm.resetFields()
    },
    // 修改弹窗确定事件
    editCate() {
      this.$refs.editCateRuleForm.validate(async (valid) => {
        if (!valid) return ''
        const { data: res } = await editCateGories(this.editCateForm.id, this.editCateForm.cat_name)
        if (res.meta.status !== 200) return this.$message.error('修改分类名称失败')
        this.$message.success('修改分类名称成功')
        this.getCateLists()
        this.editCateDialogVisible = false
      })
    },
    // 删除分类
    async delCate(val) {
      const ress = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => err)
      if (ress === 'confirm') {
        const { data: res } = await deleteCateGories(val.cat_id)
        if (res.meta.status !== 200) return this.$message.error('删除分类失败')
        this.$message.success('删除分类成功')

        this.getCateLists()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.tree-table {
  margin-top: 15px;
}
.elCascader {
  width: 100%;
}
</style>
